﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmNhaXuatBan : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo biến

        NhaXuatBan_BLL nxb_bll = new NhaXuatBan_BLL();
        DataTable dt = new DataTable();
        TextMessageBox box = new TextMessageBox();
        public string maNXB = "";
        private bool isOK = false;
        #endregion

        #region LOadForm
        public frmNhaXuatBan()
        {
            InitializeComponent();
        }

        private void frmNhaXuatBan_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region các  chức năng

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
                
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        #endregion
        #region các sự kiện
        private void frmNhaXuatBan_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }
        #endregion

        #region hàm viết thêm
        private void UpdateData()
        {
            if (checkValid())
            {
                NhaXuatBan_Public nxb = new NhaXuatBan_Public();
                if (maNXB == "")
                {
                    if (checkTenInsert())
                    {
                        nxb.MaNhaXB = box.AutoID("NXB", 3, nxb_bll.LayDuLieu());
                        nxb.TenNhaXB =box.toUpper( txtTenNXB.Text);
                        nxb.GhiChu = txtGhiChu.Text;

                        nxb_bll.Insert(nxb, nxb, nxb);
                        CloseForm();
                    }
                }
                else
                {
                    nxb.MaNhaXB = txtMaNXB.Text.Trim();
                    nxb.TenNhaXB =box.toUpper(txtTenNXB.Text);
                    nxb.GhiChu = txtGhiChu.Text;

                    nxb_bll.Update(nxb, nxb, nxb);
                    CloseForm();
                }

            }
        }
        private void CloseForm()
        {
            if (isOK == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }
        private void LoadData()
        {
            if (maNXB != "")
            {
                dt = nxb_bll.TaoBang(box.sqlCheckExist("DM_NhaXuatBan","MaNhaXB",maNXB));
                DataRow dr = dt.Rows[0];
                txtMaNXB.Text = maNXB;
                txtTenNXB.Text=dr["TenNhaXB"].ToString();
                txtGhiChu.Text = dr["GhiChu"].ToString();
            }
        }
        private void SetInterface()
        {
            txtMaNXB.Text = "Mã tự sinh";
            txtTenNXB.Text = "";
            txtGhiChu.Text = "";
            txtTenNXB.Focus();
            maNXB = "";
        }
        private bool checkValid()
        {
            if (txtTenNXB.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên nhà xuất bản.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTenNXB.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            dt = nxb_bll.TaoBang(box.sqlCheckExist("DM_NhaXuatBan","TenNhaXB",txtTenNXB.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTenNXB.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtTenNXB.Focus();
                return false;
            }
            return true;
        }

        #endregion
    }
}